package com.kstech.test.wqc.View;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kstech.test.wqc.R;
import com.kstech.test.wqc.listener.ViewDataChangeListener;


/**
 * Created by lenovo on 2016/11/2.
 */

public class DataShowView extends LinearLayout implements ViewDataChangeListener{
    private TextView tv;
    private TextView tvName;
    private TextView tvUnit;
    private Activity activity;

    public DataShowView(Activity context) {
        super(context);
        this.activity = context;
        initView();
    }
    public DataShowView(Activity context,String itemName) {
        super(context);
        this.activity = context;
        initView();
        tvName.setText(itemName);
    }

    public DataShowView(Activity context, AttributeSet attrs) {
        super(context, attrs);
        this.activity = context;
        initView();
    }

    public DataShowView(Activity context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.activity = context;
        initView();
    }

    private void initView(){
        View view = View.inflate(getContext(), R.layout.datashow,null);
        tv = (TextView) view.findViewById(R.id.tv_data);
        tvName = (TextView) view.findViewById(R.id.tv_name);
        tvUnit = (TextView) view.findViewById(R.id.tv_unit);
        this.addView(view,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }
    public void setTvName(String values){
        tvName.setText(values);
    }
    public void setTvUnit(String values){
        tvUnit.setText(values);
    }
    public String getValue(){
        return tv.getText().toString();
    }
    public String getItem(){
        return tvName.getText().toString();
    }

    @Override
    public void onDataChanged(final String value) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv.setText(value);
            }
        });
    }
}
